<?php
	
	$superCategoryID = $categoryID = $subcategoryID = $bestCategoryID = false;
	
	$superCategory = $category = $subcategory = $categoryURL = false;
	
	if(getRequestParameter("super_category"))
	{
		$superCategory = get_category(getRequestParameter("super_category"), $_SESSION["language"]["id"]);
		if($superCategory)
		{
			$bestCategory = $superCategory;
			$superCategoryID = $bestCategoryID = $superCategory["id"];
			$categoryURL = site_url().$superCategory["url"]."/";
		}
		
	}
	
	if(getRequestParameter("category"))
	{
		$category = get_category(getRequestParameter("category"), $_SESSION["language"]["id"]);
		if($category)
		{
			$bestCategory = $category;
			$categoryID = $bestCategoryID = $category["id"];
			$categoryURL .= $category["url"]."/";
		}
	}
	
	if(getRequestParameter("sub_category"))
	{
		$subcategory = get_category(getRequestParameter("sub_category"), $_SESSION["language"]["id"]);
		if($subcategory)
		{
			$bestCategory = $subcategory;
			$subcategoryID = $bestCategoryID = $subcategory["id"];
			$categoryURL .= $subcategory["url"]."/";
		}
	}
	
	$relatedCategoryIDS = $connection->fetch_result("SELECT c.id, c.parent_id, c.level FROM categories c 
		                                              LEFT JOIN products_categories pc ON pc.category_id = c.id 
		                                              LEFT JOIN products p ON pc.product_id = p.id
		                                             WHERE p.shop_id = ? GROUP BY c.id ORDER BY c.level", array($page["id"]));
	
	$relatedCategories = $categories;
	
	foreach($relatedCategories as $key => $cat)
	{
		$showSuperCat = false;
		
		if(isset($cat["children"]))
		{
			foreach($cat["children"] as $cKey => $childCat)
			{
				$showCategory = false;
				if(isset($childCat["children"]))
				{
					foreach($childCat["children"] as $sKey => $subCat)
					{
						$showSubCategory = false;
						
						foreach($relatedCategoryIDS as $relatedID)
						{
							if($relatedID["id"] == $subCat["id"])
							{
								$showSubCategory = $showCategory = $showSuperCat = true;
							}
						}
						
						if($showSubCategory == false)
						{
							unset($relatedCategories[$key]["children"][$cKey]["children"][$sKey]);
						}
					}
				}
				if($showCategory == false)
				{
					foreach($relatedCategoryIDS as $relatedID)
					{
						if($relatedID["id"] == $childCat["id"])
						{
							$showCategory = $showSuperCat = true;
						}
					}
				}
				
				if($showCategory == false)
				{
					unset($relatedCategories[$key]["children"][$cKey]);
				}
				
			}
		}
		
		if($showCategory == false)
		{
			foreach($relatedCategoryIDS as $relatedID)
			{
				if($relatedID["id"] == $cat["id"])
				{
					$showSuperCat = true;
				}
			}
		}
		
		if($showSuperCat == false)
		{
			unset($relatedCategories[$key]);
		}
	}
?>
<!-- SIDEBAR -->
<?php include("html-includes/company/items/sidebar.php"); ?>
<!-- END SIDEBAR-->
<div class="col-md-9">
		<div class="row">
			<?php include("global-includes/breadcrumbs.php"); ?>
		</div>
		
		<h1 class="sectionTitle">
			<?php
				if($bestCategory)
				{
					echo ucfirst(((translate("products")))). " &quot;" . ucfirst($bestCategory["name"])."&quot; - ";
				}
			?>
			<?php echo $page["name"]; ?>
		</h1>
		<?php if(!$bestCategory): ?>
			<div class="companyDetails">
				<?php
					if($page["business_type"] == 1)
					{
						$businessType = translate("producator");
					}
					elseif($page["business_type"] == 2)
					{
						$businessType = translate("distribuitor");
					}
					elseif($page["business_type"] == 3)
					{
						$businessType = translate("furnizor");
					}
									
				?>	
				<div class="pull-left"><span class="glyphicon glyphicon-map-marker"></span> <?php echo ucfirst(translate("city")); ?>: <span><?php echo $page["city_name"]; ?></span></div>
				<div class="pull-left"><span class="glyphicon glyphicon-tower"></span> <?php echo ucfirst(translate("business_type")); ?>: <span><?php echo ucfirst($businessType);?></span></div>
				<div class="clearfix"></div>
			</div>
			<?php
				echo $page["description"];
			?>
		<?php else: ?>
			<a href="<?php echo $categoryURL; ?>" title="<?php echo $bestCategory["name"]; ?>" class="btn btn-primary btn-sm"> <?php echo translate("vezi_toate_produsele_din"); ?> <?php echo $bestCategory["name"]; ?></a>
		<?php endif; ?>
		<div class="clearfix"></div>
		<br />
	<?php
		$itemsPerPage = 20;
		
		$products = filter_products($bestCategoryID, false, null, $itemsPerPage, "id", "asc", 0, false, $page["id"]);
		if($products)
		{
			$totalPages = ceil(($products[0]["num_rows"]/$itemsPerPage));
			$currentPage = 1;
			if(getRequestParameter("p") && getRequestParameter("p") > 1)
			{
				$currentPage = getRequestParameter("p");
			}
			 
			$totalItems = 0;
			if(isset($products[0]["num_rows"]))
			{
				$totalItems = $products[0]["num_rows"];
			}
		?>
			<div class="col-md-12">
				<?php $details = 0; $showContact = false; include("global-includes/sort-bar.php"); ?>
			</div>
			<!-- PRODUCTS LIST -->
			<div class="productHolder cols-4" type="0" items="<?php echo $settings["number_of_products"];?>" orderby="default" sort_order="asc">
				<?php include("html-includes/common/product-list.php"); ?>
			</div>
			<!-- END PRODUCTS LIST -->
			
			<?php $showContact = false; include("global-includes/pagination.php"); ?>
	<?php } ?>
</div>

<div class="clearfix"></div>

<input type="hidden" id="company_id" value="<?php echo $page["id"]; ?>" />
<input type="hidden" id="parent_category_id" value="<?php echo $superCategoryID?>" />
<input type="hidden" id="category_id" value="<?php echo $categoryID?>" />
<input type="hidden" id="subcategory_id" value="<?php echo $subcategoryID; ?>" />

<input type="hidden" id="product_type" value="" />
<input type="hidden" id="filters" value="" /> 
<input type="hidden" id="page_no" value="1" /> 